﻿using System.Diagnostics;
using System.IO;
using System.Reflection;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace Vcm
{
    public class ProxyDocument
    {
        public Document Document;
        public int ImageIndex;
        public PdfWriter Writer;
        public string ArtPath;
        public string Copyright;
        public string Watermark;
        public CardPosition WatermarkPosition;
        public TextOrientation WatermarkOrientation;
        public int WatermarkColor;
        public int WatermarkOpacity;
        public string ExpansionSymbolPath;
        public bool UseGhost;
        public bool NotForLegalPlay;
        public int CardsPerPage;

        public static ProxyDocument CreateProxiesPdf(string proxyFileName, string artPath, string copyright, string watermark, CardPosition watermarkPosition, TextOrientation watermarkOrientation, int watermarkColor, int watermarkOpacity, string expansionSymbolPath, bool useGhost, bool notForLegalPlay, int cardsPerPage)
        {
            CardMakerBase.ZoomFactor = cardsPerPage == 9 ? 1 : 3;

            var doc = cardsPerPage == 9
                ? new Document(PageSize.A4)
                : new Document(new RectangleReadOnly(CardMakerBase.LeftPageMargin * 2f / CardMakerBase.ZoomFactor + CardMakerBase.CardWidth, CardMakerBase.BottomPageMargin * 2f / CardMakerBase.ZoomFactor + CardMakerBase.CardHeight));
            // create the pdf document
            var writer = PdfWriter.GetInstance(doc, new FileStream(proxyFileName, FileMode.Create));
            doc.Open();

            Assembly assembly = Assembly.GetExecutingAssembly();
            if (!Path.IsPathRooted(artPath))
            {
                artPath = artPath.Length > 0
                    ? Path.GetDirectoryName(assembly.Location) + "\\" + artPath
                    : Path.GetDirectoryName(assembly.Location);
            }

            if (!File.Exists(expansionSymbolPath))
                expansionSymbolPath = null;

            doc.AddTitle(proxyFileName);
            FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
            string version = fvi.ProductVersion;
            doc.AddCreator("Ankha VCM " + version);
            doc.AddAuthor(copyright);
            doc.AddCreationDate();

            return new ProxyDocument
            {
                Document = doc,
                ImageIndex = 0,
                Writer = writer,
                ArtPath = artPath,
                Watermark = watermark,
                WatermarkPosition = watermarkPosition,
                WatermarkOrientation = watermarkOrientation,
                WatermarkColor = watermarkColor,
                WatermarkOpacity = watermarkOpacity,
                Copyright = copyright,
                ExpansionSymbolPath = expansionSymbolPath,
                UseGhost = useGhost,
                NotForLegalPlay = notForLegalPlay,
                CardsPerPage = cardsPerPage
            };
        }

        public static void SaveAndCloseProxiesPdf(ProxyDocument proxyDocument)
        {
            proxyDocument.Document.Close();
        }

        public static void EnsureDocumentCanBeClosed(ProxyDocument proxyDocument)
        {
            proxyDocument.Document.NewPage();
            var cb = proxyDocument.Writer.DirectContent;
            cb.SetRGBColorStroke(255, 0, 0);
            cb.Rectangle(0, 0, 1, 1);
            cb.Stroke();
        }
    }
}